// hunter.txt
// Important script. Functions like basicnpc, but with a key difference. If it 
// is hostile, it will hunt down the party to attack. Note that this creature
// doesn't default to acting from a long way away. This needs to be set
// manually with the set_act_at_dist call.

// if you don't want it to attack right away, set its attitude to be neutral and 
//  change its attitude with a call when you want it to attack.

// Memory Cells:
//   Cell 0 - How creature moves.
//     0 - If 0, wander randomly. 
//     1 - Stands still until a target appears.
//     2 - Completely immobile, even if target appears.
//   Cell 1,2 - Stuff done flag. If both 0, nothing. Otherwise when this 
//     is killed, set to 1. (Example: If cell 1 is 3 and cell 2 is 5, when
//     creature is killed, sets SDF(3,5) to 1.)
//   Cell 3 - Dialogue node to start with if talked to. if left at 0, this
//     character doesn't talk.
//   Cell 4 - SDF

begincreaturescript;

variables;

short i,target;
short is_active = 1;
short saw_dude = 0;
short dead_count = 0;
short last_abil;
short times_killed = 0;

body;

beginstate INIT_STATE;
	set_name(ME,"Scourge Zombie");
	change_max_health(ME,70);
	set_char_unkillable(ME,1);
	sf(45,get_memory_cell(4),0);
	sf(45,28,0);
	set_act_at_dist(ME,1);
	set_attack_range(ME,5);

	last_abil = get_current_tick();

	if (get_memory_cell(0) == 2)
		set_walk_speed(ME,0);


	if (gf(45,15) > 0) {
		erase_char(ME);
		end();
		}
	break;

beginstate DEAD_STATE;
	// Set the appropriate stuff done flag for this character being dead
	if ((get_memory_cell(1) != 0) || (get_memory_cell(2) != 0))
		inc_flag(get_memory_cell(1),get_memory_cell(2),1);
break;

beginstate START_STATE; 
	if (gf(45,15) > 0) {
		erase_char(ME);
		end();
		}


	if (((is_combat()) && (tick_difference(last_abil,get_current_tick()) > 0)) || ((is_town()) && (tick_difference(last_abil,get_current_tick()) > 3))) {
		if (is_active == 0) {
			last_abil = get_current_tick();
			dead_count = dead_count - 1;
			run_text_on_char(ME,"Gloop.",0);
			if (dead_count == 0) {
				is_active = 1;
				heal_char(ME,500);
				//relocate_char(ME,50,20 + get_memory_cell(4) - 20,0);
				set_attitude(ME,10);
				set_terrain_string("You hear a new source of angry moaning nearby.");
				end();
				}
			}
		else {
			if (get_health(ME) < get_max_health(ME) / 3) {
				times_killed = times_killed + 1;
				if (times_killed == 5)
					set_char_unkillable(ME,0);
				dead_count = 5 - difficulty();
				is_active = 0;
				set_terrain_string("A zombie collapses and begins to reform.");
				play_sound(340);
				last_abil = get_current_tick();
				
				//relocate_char(ME,50,20 + get_memory_cell(4) - 20,0);
				run_text_on_char(ME,"Reforming!",0);
				set_attitude(ME,4);

				if (gf(45,get_memory_cell(4)) == 0) {
					inc_flag(45,28,1);
					sf(45,get_memory_cell(4),1);
					}
				
				if (gf(45,8) == 0) {
					sf(45,8,1);
					begin_talk_mode(15);
					}
					else {
						end();
						}
				}
				else {
					set_attitude(ME,10);
					}
			}
		
			}
		


	if (is_active == 0)	
		end();



	// if I have a target for some reason, go attack it
	if (target_ok()) {
		if (dist_to_char(get_target()) <= 16)
			set_state(3);
			else set_foe_target(ME,-1);
		}
	
	// Look for a target, attack it if visible
	if (get_foe_target(ME,12,0)) {
		if (dist_to_party() > 8)
			approach_char(ME,30000,7);
			else {
				do_attack();
				set_state(3);
				}
		}
		
	// Have I been hit? Strike back!
	if (who_shot_me() >= 0) {
		set_foe_target(ME,who_shot_me());
		do_attack();
		set_state(3);
		}

	// if we're hostile, hunt party down
	if ((get_attitude(ME) >= 10) && (saw_dude > 0)) {
		set_foe_target(ME,random_group_member(0));
		set_state(3);
		}
		else fidget(ME,25);

	// if we're in combat and the above didn't give me anything to do, just
	// stop now. Otherwise, game will keep running script, and that eats up CPU time.
	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking
	if ((get_health(ME) < get_max_health(ME) / 3) && (is_active > 0)) {
		dead_count = 5;
		is_active = 0;
		set_terrain_string("A zombie collapses and begins to reform.");
		play_sound(340);

		last_abil = get_current_tick();
		
		//relocate_char(ME,50,20 + get_memory_cell(4) - 20,0);
		run_text_on_char(ME,"Reforming!",0);
		set_attitude(ME,4);

		if (gf(45,get_memory_cell(4)) == 0) {
			inc_flag(45,28,1);
			sf(45,get_memory_cell(4),1);
			}
		
		if (gf(45,8) == 0) {
			sf(45,8,1);
			begin_talk_mode(15);
			}
			else {
				end();
				}
		}
	if (get_health(ME) < get_max_health(ME) / 3)  {
		set_state(START_STATE);
			}		
	if (saw_dude == 0) {
		if (gf(45,get_memory_cell(4)) == 0) {
			inc_flag(45,28,1);
			sf(45,get_memory_cell(4),1);
			}
		saw_dude = 1;
				if (gf(45,9) == 0) {
					sf(45,9,1);
					begin_talk_mode(14);
					}
		}
	if (is_active == 0)	
		set_state(START_STATE);
	if (gf(45,15) > 0) {
		erase_char(ME);
		end();
		}
	if (target_ok() == FALSE)
		set_state(START_STATE);
	do_attack();
break;

beginstate EVERY_TURN_STATE;

	//	run_char_animation(3,1,35);	
	//	pc_heard_sound_delay(700,100);	
	//	end();
	//	}

	//	run_char_animation(3,1,35);	
	//	pc_heard_sound_delay(700,100);	

	//	summon_creature(which_char);
	//	set_summon_level(which_char,1);	
	//	place_particle_num(ME,1077,9,8);
	//	run_text_on_char(which_char,"Bamf!",0);

	//run_text_on_char(short which_char_or_group,char *text,short color)
	//add_ability(, 15, which_ability, 0);
	
	//if ((mess1 == 0) && (get_health(ME) < (get_max_health(ME) * 5) / 10)) {
	//	mess1 = 1;
	//	}
break;

beginstate TALKING_STATE;
	if (get_memory_cell(3) == 0) {

		print_str("Talking: You get no response.");
		end();
		}
	begin_talk_mode(get_memory_cell(3));
break;